﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wechat.Util;
using Wechat.Util.ParmsConfig;
using Wechat.Util.WechatUtil;
using WeChat.Web.Filter;
using Wechat.Dto.Request;
using Wechat.Util.StringUtil;
using Wechat.Util.ParmsConfig.ParmsConvert;
using Wechat.Dto.Search;
using Wechat.Util.ReturnMessage;
using Wechat.Util.StateCode;
using Newtonsoft.Json;

namespace WeChat.Web.Controllers
{
    public class WechatController : BaseController
    {
        // GET: Wechat

        #region 页面

        /// <summary>
        /// 页面-微信菜单管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        ///页面-图文消息管理 
        /// </summary>
        /// <returns></returns>
        public ActionResult ImageTextList()
        {
            return View();
        }

        /// <summary>
        /// 页面-单图文消息管理
        /// </summary>
        /// <returns></returns>
        public ActionResult EditImageText(int imageTextId = 0)
        {
            ViewBag.imageTextId = imageTextId;
            return View();
        }

        /// <summary>
        /// 页面-多图文消息管理
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public ActionResult EditImageTextGroup(int groupId = 0)
        {
            ViewBag.GroupId = groupId;
            return View();
        }

        /// <summary>
        /// H5页面
        /// </summary>
        /// <returns></returns>
        public ActionResult H5News(int id = 0)
        {
            id = id == 0 ? 1001 : id;
            ViewBag.Id = id;
            return View();
        }

        /// <summary>
        /// 页面-关注时默认回复消息管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AutoReply(int id = 0)
        {
            ViewBag.Id = id;
            return View();
        }
        #endregion

        #region 功能列表

        /// <summary>
        /// 获取微信菜单列表
        /// </summary>
        /// <returns></returns>
        [ValidateInput(false)]
        public JsonResult WechatMemuAjax()
        {
            var data = string.Empty;
            var sub = Request["sub"];
            if (this.GetType().GetMethod(sub) != null)
            {
                try
                {
                    data = this.GetType().GetMethod(sub).Invoke(this, null).ToString();
                }
                catch (Exception ee)
                {
                    data = JsonConvert.SerializeObject(new
                    {
                        status = false,
                        code = (int)EnumStateCode.SystemError,
                        message = ee.Message
                    });
                }

            }
            else
            {
                data = JsonConvert.SerializeObject(new
                {
                    status = false,
                    code = (int)EnumStateCode.ParameterInvalid,
                    message = "请求的方法不存在，请联系管理员"
                });
            }
            return Json(data);
        }


        /// <summary>
        /// 获取微信菜单
        /// </summary>
        /// <returns></returns>
        public string getMemu()
        {
            var postUrl = ParmsConfigUtil.WechatApiUrl + "/api/wechatmemu/getmemu";
            return WebRequestSend(postUrl, null, "get");
        }

        /// <summary>
        /// 同步微信菜单
        /// </summary>
        /// <returns></returns>
        public string synchronizeMemu()
        {
            var postUrl = ParmsConfigUtil.WechatApiUrl + "/api/wechatmemu/synmemu";
            return WebRequestSend(postUrl, null, "get");
        }

        /// <summary>
        /// 删除微信菜单
        /// </summary>
        /// <returns></returns>
        public string deleteMemu()
        {
            int WechatId = 0;
            WechatId = WechatId.FoInt("WechatId");
            var postUrl = ParmsConfigUtil.WechatApiUrl + "/api/wechatmemu/deletememu?ids=" + WechatId;
            return WebRequestSend(postUrl);
        }

        /// <summary>
        /// 添加或修改菜单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public string insertOrUpdateMemu()
        {
            WechatMemuRequest request = new WechatMemuRequest();
            request.WechatId = request.WechatId.FoInt("WechatId");
            request.WechatName = request.WechatName.FoString("WechatName");
            request.WechatSort = request.WechatSort.FoInt("WechatSort");
            request.WechatType = request.WechatType.FoString("WechatType");

            if (request.WechatType == "view")
            {
                request.WechatUrl = Request["WechatUrl"];
            }
            else if (request.WechatType == "click")
            {

            }
            return string.Empty;
        }

        /// <summary>
        /// 添加或修改图文消息组
        /// </summary>
        /// <returns></returns>
        public string insertOrUpdateImageTextGroup()
        {
            return insertOrUpdateImageText(false);
        }

        /// <summary>
        /// 添加或修改图文消息
        /// </summary>
        /// <param name="IsSingle">是否为单图文</param>
        /// <returns></returns>
        public string insertOrUpdateImageText(bool IsSingle = true)
        {
            WechatImageTextRequest request = new WechatImageTextRequest();
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("Id", request.Id.FoString("Id", true));
            parameters.Add("GroupId", request.GroupId.FoString("GroupId", true));
            if (!string.IsNullOrWhiteSpace(Request["PicUrl"]))
            {
                request.PicUrl = Request["PicUrl"];

                if (!request.PicUrl.StartsWith("http://"))
                {
                    request.PicUrl = ParmsConfigUtil.WebSiteUrl + request.PicUrl;
                }
            }
            parameters.Add("PicUrl", request.PicUrl);
            parameters.Add("Title", request.Title.FoString("Title"));
            parameters.Add("Descriptions", request.Descriptions.FoString("Descriptions"));
            parameters.Add("Content", request.Content.FoString("Content"));
            parameters.Add("RedictUrl", request.RedictUrl.FoString("RedictUrl"));
            var data = string.Empty;
            if (IsSingle)
            {
                parameters.Add("MsgType", "SINGLE");
                var postUrl = ParmsConfigUtil.WechatApiUrl + "/api/imageText/insertorupdate";
                data = WebRequestSend(postUrl, parameters);
            }
            else
            {
                parameters.Add("MsgType", "MULTIPLY");
                var postUrl = ParmsConfigUtil.WechatApiUrl + "/api/imagetextgroup/insertorupdate";
                data = WebRequestSend(postUrl, parameters);
            }

            return data;
        }

        /// <summary>
        /// 获取图文信息
        /// </summary>
        /// <returns></returns>
        public string getImageText()
        {
            WechatImageTextSearch search = new WechatImageTextSearch();

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("Id", search.Id.FoString("Id"));
            parameters.Add("GroupId", search.GroupId.FoString("GroupId", true));
            var postUrl = ParmsConfigUtil.WechatApiUrl + "/api/imagetext/get";
            return WebRequestSend(postUrl, parameters);
        }

        /// <summary>
        /// 删除图文信息
        /// </summary>
        /// <returns></returns>
        public string deleteImageText()
        {
            int Id = 0;
            var postUrl = ParmsConfigUtil.WechatApiUrl + $"api/imagetext/delete?ids={Id.FoString("Id", true)}";
            return WebRequestSend(postUrl);
        }

        /// <summary>
        /// 获取微信图文消息列表
        /// </summary>
        /// <returns></returns>
        public string getImageTextList()
        {
            WechatImageTextSearch search = new WechatImageTextSearch();

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("Id", search.Id.FoString("Id"));
            parameters.Add("GroupId", search.GroupId.FoString("GroupId", true));
            var postUrl = ParmsConfigUtil.WechatApiUrl + "/api/imagetext/getlist";
            return WebRequestSend(postUrl, parameters);
        }

        /// <summary>
        /// 添加或修改关注默认回复
        /// </summary>
        /// <returns></returns>
        public string insertOrUpdateReply()
        {
            WechatAutoReplyRequest request = new WechatAutoReplyRequest();

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("AutoReplyId", request.AutoReplyId.FoString("AutoReplyId"));
            parameters.Add("Content", request.Content.FoString("Content"));
            parameters.Add("GroupId", request.GroupId.FoString("GroupId"));
            parameters.Add("ImageTextId", request.ImageTextId.FoString("ImageTextId"));
            parameters.Add("ReplyType", request.ReplyType.FoString("ReplyType"));
            var postUrl = ParmsConfigUtil.WechatApiUrl + "/api/reply/insertorupdate";
            return WebRequestSend(postUrl, parameters);
        }


        #endregion

        #region 请求

        /// <summary>
        /// 请求
        /// </summary>
        /// <param name="postUrl"></param>
        /// <param name="parameters"></param>
        /// <param name="method"></param>
        /// <returns></returns>
        public string WebRequestSend(string postUrl, Dictionary<string, string> parameters = null, string method = "post")
        {
            WebRequestUtil webRequestUtil = new WebRequestUtil(postUrl, method);
            if (parameters != null)
                webRequestUtil.parameters = parameters;
            var data = webRequestUtil.SendPost();
            return data;
        }
        #endregion
    }
}